package org.ty.example.provider;

import org.ty.example.common.service.UserService;
import org.ty.rpc.core.RpcApplication;
import org.ty.rpc.core.config.RegistryConfig;
import org.ty.rpc.core.config.RpcConfig;
import org.ty.rpc.core.model.ServiceMetaInfo;
import org.ty.rpc.core.resgistry.LocalRegistry;
import org.ty.rpc.core.resgistry.Registry;
import org.ty.rpc.core.server.HttpService;
import org.ty.rpc.core.server.VertxHttpServer;
import org.ty.rpc.core.spi.RegistryFactory;


/**
 * @Author: WangJiYou
 * @Date: 2024/12/22/21:38
 * @Description:
 */
public class EasyProviderExample {

    public static void main(String[] args) {

        // RPC 框架初始化
        RpcApplication.init();

        // 注册服务
        String serviceName = UserService.class.getName();
        LocalRegistry.register(serviceName, UserServiceImpl.class);

        // 注册服务到注册中心
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 启动 web 服务
        HttpService httpService = new VertxHttpServer();
        httpService.doStart(8080);
    }
}
